/**
 * 
 */
package smart_campus_api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zhiteng.entity.Parent;
import com.zhiteng.entity.School;
import com.zhiteng.entity.Teacher;
import com.zhiteng.entity.Term;
import com.zhiteng.entity.User;
import com.zhiteng.entity.Year;
import com.zhiteng.service.ParentService;
import com.zhiteng.service.SchoolService;
import com.zhiteng.service.TeacherService;
import com.zhiteng.utils.common.HttpClientUtil;
import com.zhiteng.utils.tencent.campus.CommonResult;
import com.zhiteng.utils.tencent.campus.CommonResult2;
import com.zhiteng.utils.tencent.campus.Data;
import com.zhiteng.utils.tencent.campus.SignUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/*.xml")
public class ImportData3 {
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private ParentService parentService;
	
	@Value("${devCode}")
	private String devCode;
	@Value("${devType}")
	private String devType;
	@Value("${objType}")
	private String objType;
	@Value("${officeid}")
	private String officeid;
	@Value("${keyForData}")
	private String keyForData;
	
	//	将所有学校的所有家长信息导入到数据库
	@Test
	public void importAllParent() {
		long timestamp = System.currentTimeMillis();
		List<Parent> allParentList = new ArrayList<>();
		//	获取所有学校
		List<School> schoolList = schoolService.getAllSchool();
		if(schoolList!=null && !schoolList.isEmpty()) {
			for (School school : schoolList) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("timestamp", timestamp);
				map.put("devCode", devCode);
				map.put("devType", devType);
				map.put("objType", objType);
				map.put("officeid", officeid);
				map.put("objectid", school.getObjectid());
				map.put("size", "10000");
				String url = "https://open.campus.qq.com/api/open/buGetParentList?" 
							+ "timestamp=" + timestamp 
							+ "&sign=" + SignUtil.getSign(map,keyForData) 
							+ "&devCode=" + devCode
							+ "&devType=" + devType
							+ "&objType=" + objType
							+ "&officeid=" + officeid
							+ "&objectid=" + school.getObjectid()
							+ "&size=10000";
				String jsonStr = HttpClientUtil.get(url);	//台州市椒江区洪家街道中心小学
				//System.out.println(school.getName()+":"+jsonStr);
				
				//	把json数据封装成CommonResult
				CommonResult<Parent> result = JSON.parseObject(jsonStr, new TypeReference<CommonResult<Parent>>(){});
				//	获取该学校的所有家长
				if(result!=null && result.getData()!=null) {
					List<Parent> parentList = result.getData().getDataList();
					if(parentList!=null && !parentList.isEmpty()) {
						allParentList.addAll(parentList);
						System.out.println(allParentList.size());
					}
				}
			}
			int count = parentService.saveBatch(allParentList);
			long end = System.currentTimeMillis();
			System.out.println("成功插入了"+count+"条数据！");
			System.out.println("一共耗时"+(end-timestamp)+"毫秒！"); //135644s
		}
		
		/*long endTime1 = System.currentTimeMillis();
		int count = teacherService.saveTeacherBatch(teacherList);
		System.out.println("成功插入了"+count+"条数据！");
		long endTime2 = System.currentTimeMillis();
		System.out.println("查询及封装共耗时"+(endTime1-startTime)+"毫秒！"); //132606s
		System.out.println("插入共耗时"+(endTime2-endTime1)+"毫秒！"); //3038s
		System.out.println("一共耗时"+(endTime2-startTime)+"毫秒！"); //135644s
*/	}
	

}
